package com.controller;
import cn.hutool.core.util.StrUtil;
import com.entity.BloggerMessage;
import com.service.BloggerMessageService;
import com.system.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bloggerMessage")
public class BloggerMessageController {
    @Autowired
    private BloggerMessageService bloggerMessageService;

    /**
     * 添加留言
     * @param bloggerMessage
     * @return
     */
    @RequestMapping("/addMessage.do")
    @ResponseBody
    public Map<String,Object> addMessage(BloggerMessage bloggerMessage){
        Map<String, Object> result = new HashMap<>();
        System.out.println(bloggerMessage.toString());
        if(StrUtil.isEmpty(bloggerMessage.getMessage())){
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.PARAM_ERROR);
            return result;
        }
        try {
            // 业务层写在try catch块中
            bloggerMessageService.addMessage(bloggerMessage);
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);

        }catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
        }
        return result;
    }

    @RequestMapping("/bloggerMessageIndex")
    public ModelAndView bloggerMessageIndex() throws Exception{
        ModelAndView mav=new ModelAndView();
        mav.addObject("pageTitle", "关于博主—倾城博客系统");
        mav.addObject("mainPage", "/foreground/blogger/showMessage.jsp");
        mav.setViewName("pages/blogg/mainTemp");
        return mav;
    }

    @RequestMapping("/showMessage.do")

    public ModelAndView showMessage(BloggerMessage bloggerMessage){
        ModelAndView mav=new ModelAndView();
        List<BloggerMessage> bloggerMessageList = bloggerMessageService.getBloggerMessageList(bloggerMessage);
        mav.addObject("data", bloggerMessageList);
        mav.addObject("mainPage", "/foreground/blogger/showMessage.jsp");
        mav.setViewName("pages/blogg/mainTemp");
        return mav;
    }
}
